package com.ncoor.core.path;

import com.ncoor.core.pathPoint.Node;
import com.ncoor.core.pathPoint.PathPoint;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * 以每个图形点描述一个路径
 * @author Rock Chin
 */
public class Path {
	private Comparator<PathPoint> comparator=new Comparator<PathPoint>() {
		@Override
		public int compare(PathPoint o1, PathPoint o2) {
			if(o1.getZ()>o2.getZ()){
				return -1;
			}else if(o1.getZ()<o2.getZ()){
				return 1;
			}else {
				if(o1.getX()>o2.getX()){
					return -1;
				}else if(o1.getX()<o2.getX()){
					return 1;
				}else {
					if(o1.getY()>o2.getY()){
						return -1;
					}else if(o1.getY()<o2.getY()){
						return 1;
					}else {
						return -1;
					}
				}
			}
		}
	};
	public void setComparator(Comparator<PathPoint> comparator){
		this.comparator=comparator;
	}
	public Path(){
		;
	}
	/**
	 * 使用PathPoint类储存了所有的关键点
	 */
	private ArrayList<PathPoint> keyPoints=new ArrayList<>();

	/**
	 * 根据index来获取关键点对象，关键点默认自动排序方式:z+先于z-,x+先于x-,y+先于y-
	 * @param index
	 * @return
	 */
	public PathPoint getPoint(int index){
		return keyPoints.get(index);
	}

	/**
	 * 添加关键点,自动排序
	 * @param point
	 * @return
	 */
	public int addPoint(PathPoint point){
		keyPoints.add(point);
		keyPoints.sort(comparator);
		return keyPoints.indexOf(point);
	}
	public int addNodePoint(int z,int x,int y){
		return addPoint(new Node(z,x,y));
	}

	/**
	 * 删除一个关键点
	 * @param index
	 * @return
	 */
	public PathPoint removePoint(int index){
		return keyPoints.remove(index);
	}
}
